package com.jereh.topnews.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jereh.topnews.R;

public class ContextMenuActivity extends AppCompatActivity {

    private TextView test1;
    private ImageView test2;
    private Button test3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_context_menu);
        test1=(TextView)findViewById(R.id.test1);
        test2=(ImageView)findViewById(R.id.test2);
        test3=(Button)findViewById(R.id.test3);
        //注册上下文菜单绑定到一个view上
        registerForContextMenu(test1);
        registerForContextMenu(test2);
        registerForContextMenu(test3);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.add(Menu.NONE,0,0,"选项1");
        menu.add(Menu.NONE,1,1,"选项2");
        menu.add(Menu.NONE,2,2,"选项3");
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case 0:
                Toast.makeText(ContextMenuActivity.this, "选中选项一", Toast.LENGTH_SHORT).show();
                break;
            case 1:
                Toast.makeText(ContextMenuActivity.this, "选中选项二", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(ContextMenuActivity.this, "选中选项三", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onContextItemSelected(item);
    }
}
